package b1.scenes.fight
{
	import Box2D.Collision.*;
	import Box2D.Collision.Shapes.*;
	import Box2D.Common.Math.*;
	import Box2D.Dynamics.*;
	
	import b1.conifg.*;
	import b1.net.*;
	import b1.scenes.*;
	
	import com.greensock.TweenLite;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.media.Sound;

	public class Launcher extends Sprite
	{
		public var launcherIndex:int;
		public var preparedBone:Bone;
		public var dragFlag:Boolean;
		//当前使用的体力
		public var currentVitality:int;
		private var boneZeroPosY:Number;
		private const maxDist:Number = 60;
		private var bowstring:Sprite;
		private var bowstringWidth:Number;
		
		public function Launcher(launcherIndex:int)
		{
			super();
			this.launcherIndex = launcherIndex;
			this.dragFlag = false;
			//加载弓
			var launcherConfig:LauncherConfig = Config.objectConfigs[launcherIndex];
			var LaucherImg:Class = ResourceHelper.getInstance().getClassByName(launcherConfig.iconImage);
			var launcher:Sprite = new LaucherImg();
			launcher.x = -launcher.width / 2;
			addChild(launcher);
			//绘制弓弦
			bowstring = new Sprite();
			bowstring.y = launcher.height - 2;
			bowstringWidth = launcher.width - 10;
			addChild(bowstring);
			drawBowstring(0);
		}
		
		//绘制弓弦
		public function drawBowstring(dist:Number):void
		{
			var launcherConfig:LauncherConfig = Config.objectConfigs[launcherIndex];
			bowstring.graphics.clear();
			bowstring.graphics.lineStyle(1.5, launcherConfig.stringColor);
			bowstring.graphics.moveTo(-bowstringWidth / 2, 0);
			bowstring.graphics.lineTo(0, dist);
			bowstring.graphics.lineTo(bowstringWidth / 2, 0);
		}
		
		//准备骨头
		public function prepareBone(boneIndex:int):void
		{
			if (preparedBone == null || boneIndex != preparedBone.index)
			{
				removePrepareBone();
				preparedBone = new Bone(boneIndex);
				boneZeroPosY = - preparedBone.height / 2;
				preparedBone.x = - preparedBone.width / 2;
				preparedBone.y = boneZeroPosY;
				addChild(preparedBone);
				preparedBone.addEventListener(MouseEvent.MOUSE_DOWN, onBoneMouseDown);
			}
		}
		
		//移除准备的骨头
		public function removePrepareBone():void
		{
			if (preparedBone != null && preparedBone.parent != null)
			{
				preparedBone.removeEventListener(MouseEvent.MOUSE_DOWN, onBoneMouseDown);
				preparedBone.parent.removeChild(preparedBone);
				preparedBone = null;
			}
		}
		
		//弓箭的最大体力
		public function get vitalityMax():int
		{
			var launcherConfig:LauncherConfig = Config.objectConfigs[launcherIndex];
			return launcherConfig.vitalityMax;
		}
		
		//按距离拉动弓弦
		public function setBoneByDistance(dist:Number, vitalityLimit:Number = -1):void
		{
			if (preparedBone == null)
				return;
			dist = Math.min(dist, maxDist);
			currentVitality = vitalityMax * dist / maxDist;
			//超过了体力极限
			if (vitalityLimit != -1 && currentVitality > vitalityLimit)
			{
				currentVitality = vitalityLimit;
				dist = maxDist * currentVitality / vitalityMax;
			}
			preparedBone.y = boneZeroPosY + dist;
			drawBowstring(dist);
		}
		
		//按体力拉动弓弦
		public function setBoneByVitality(vitality:Number):void
		{
			if (preparedBone == null)
				return;
			currentVitality = Math.min(vitality, vitalityMax);
			var dist:Number = maxDist * currentVitality / vitalityMax;
			preparedBone.y = boneZeroPosY + dist;
			drawBowstring(dist);
		}
		
		//发射准备的骨头
		public function launchPrepareBone():Number
		{
			if (preparedBone == null)
				return 0;
			var fight:Fight = SceneContainer.getInstance().currentScene as Fight;
			//由若干个骨头集结而成
			var mass:Number = 0;
			var boneConfig:ObjectConfig = Config.objectConfigs[preparedBone.index];
			var localPoint:Point = new Point(preparedBone.x, preparedBone.y);
			var globalPoint:Point = localToGlobal(localPoint);
			var bonePosX:Number = globalPoint.x;
			var bonePosY:Number = globalPoint.y;
			var bone:Bone; 
			if (boneConfig.splitBoneNum > 0)
			{
				var boneCluster:BoneCluster = new BoneCluster(bonePosX, bonePosY, boneConfig.splitBoneIndex, boneConfig.splitBoneNum);
				for (var i:int = 0; i < boneCluster.bones.length; i++)
				{
					bone = boneCluster.bones[i];
					mass += bone.objectB2Body.GetMass();
					fight.objectLayer.addChild(bone);
					TweenLite.delayedCall(6, bone.destroy); 
				} 
				TweenLite.delayedCall(1, boneCluster.split);
			}
			else
			{
				bone = new Bone(preparedBone.index);
				var boneb2Body:b2Body = bone.createB2Body(fight.world, bonePosX, bonePosY, bone.width, bone.height, boneConfig.density, boneConfig.restitution);
				boneb2Body.SetAngle(this.rotation * Math.PI / 180);
				TweenLite.delayedCall(8, bone.destroy); 
				mass += bone.objectB2Body.GetMass();
				fight.objectLayer.addChild(bone);
			}
			trace("index: " + bone.index, " mass: " + mass);
			var radians:Number = (this.rotation - 90) * Math.PI / 180;
			var forceX:Number = this.currentVitality * Math.cos(radians) * 100;
			var forceY:Number = this.currentVitality * Math.sin(radians) * 100;
			var torque:Number = b2Math.RandomRange(0, 200) * mass;
			bone.applyForce(forceX, forceY, torque)
			removePrepareBone();
			drawBowstring(0);
			//播放音效
			var SoundBow:Class = ResourceHelper.getInstance().getClassByName("SoundBow");
			var sound:Sound = new SoundBow();
			sound.play(); 
			return this.currentVitality;
		}
		
		private function onBoneMouseDown(event:MouseEvent):void
		{
			var fight:Fight = SceneContainer.getInstance().currentScene as Fight;
			if (!fight.endFightFlag)
			{
				dragFlag = true;
			}
		}
	}
}